﻿using Alipay.AopSdk.Core;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core.Response;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Configuration;
namespace Medlcalcare.MVC.Controllers
{
    public class ZFBPayController : Controller
    {

        public ActionResult Finash()
        {
            return View();
        }
        public IActionResult Index()
        {
            return View();
        }
        public string GetID()
        {
            var vNow = DateTime.Now;
            var sNow = "";
            sNow += vNow.Year;
            sNow += vNow.Month + 1;
            sNow += vNow.Day;
            sNow += vNow.Hour;
            sNow += vNow.Minute;
            sNow += vNow.Second;
            sNow += vNow.Millisecond;
            return sNow;
        }
        [HttpGet]
        public IActionResult PCPay()
        {
            DefaultAopClient client = new DefaultAopClient(config.gatewayUrl, config.app_id, config.private_key, "json", "1.0", config.sign_type, config.alipay_public_key, config.charset, false);


            // 外部订单号，商户网站订单系统中唯一的订单号
            string out_trade_no = GetID();

            // 订单名称
            string subject = "测试";

            // 付款金额
            string total_amout = "0.1";

            // 商品描述
            string body = "111111123111";

            // 组装业务参数model
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.Body = body;
            model.Subject = subject;
            model.TotalAmount = total_amout;
            model.OutTradeNo = out_trade_no;
            model.ProductCode = "FAST_INSTANT_TRADE_PAY";

            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            // 设置同步回调地址
            //request.SetReturnUrl("http://guft58.natappfree.cc/home/Finash");
            request.SetReturnUrl($"http://nexrek.natappfree.cc/home/Finash");
            // 设置异步通知接收地址
            request.SetNotifyUrl($"http://nexrek.natappfree.cc/home/Finash");
            // 将业务model载入到request
            request.SetBizModel(model);

            AlipayTradePagePayResponse response = null;
            try
            {
                response = client.PageExecute(request, null, "post");
                //Response.WriteAsync(response.Body);

                return Content(response.Body, "text/html");
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return View();
        }
    }
}
